package com.wy.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.util.StringUtil;
import com.wy.bean.Dianpu;
import com.wy.bean.Shop;
import com.wy.service.DianpuService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

@Controller
public class DianpuController {

    @Resource
    private DianpuService dianpuService;

    /**
     * 列表模糊查询
     */
    @RequestMapping("list")
    public String list(Dianpu dianpu, Model model,
                       @RequestParam(defaultValue = "1") int pageNum,
                       @RequestParam(defaultValue = "3") int pageSize){
        PageHelper.startPage(pageNum,pageSize);
        List<Dianpu> list = dianpuService.list(dianpu);
        PageInfo<Dianpu> pageInfo = new PageInfo<>(list);
        model.addAttribute("pageInfo",pageInfo);
        model.addAttribute("dianpu",dianpu);
        return "list";
    }

    /**
     * 进入新增-修改页面
     */
    @RequestMapping("toadd")
    public String toadd(String did,Model model){
        if(StringUtil.isNotEmpty(did)){
            Dianpu dianpu = dianpuService.getById(did);
            model.addAttribute("dianpu",dianpu);
        }
        return "add";
    }

    /**
     * 进入详情
     */
    @RequestMapping("detail")
    public String detail(String did,Model model){
        Dianpu dianpu = dianpuService.getById(did);
        model.addAttribute("dianpu",dianpu);
        return "detail";
    }

    /**
     * 进入新增页面
     */
    @RequestMapping("save")
    public String save(Dianpu dianpu, MultipartFile file){
        //判断图片是否上传
        if(file !=null && file.getSize()>0){
            //获取图片真实名称123.jpg
            String originalFilename = file.getOriginalFilename();
            //获取图片后缀.jpg
            String substring = originalFilename.substring(originalFilename.lastIndexOf("."));
            //生成新的唯一名称
            String saveName= UUID.randomUUID().toString()+substring;
            //保存图片
            try {
                file.transferTo(new File("D:\\myupload\\"+saveName));
                //把访问路径，放到img里边存入数据库
                dianpu.setImg("/myupload/"+saveName);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        String did = dianpu.getDid();
        if(StringUtil.isNotEmpty(did)){
            dianpuService.update(dianpu);
        }else{
            dianpuService.insert(dianpu);
        }
        return "redirect:list.do";
    }

    //ajax查询所有商品
    @RequestMapping("getShops")
    @ResponseBody
    public List<Shop> getShops(){
        return dianpuService.getShops();
    }
    //ajax删除
    @RequestMapping("deletebth")
    @ResponseBody
    public Boolean deletebth(String[] dids){
        try {
            dianpuService.deletebth(dids);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }
}
